<template>
  <div class="app-container">
    <el-form class="searchFormClass" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="配置名称" prop="hvcName">
        <el-input
          v-model="queryParams.hvcName"
          placeholder="请输入配置名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Hive版本" prop="hvVersion" label-width="80px">
        <el-input
          v-model="queryParams.hvVersion"
          placeholder="请输入Hive版本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmphive:config:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dmphive:config:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dmphive:config:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dmphive:config:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @row-dblclick="showForm" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" width="55" align="center" prop="hvcId"/>
      <el-table-column label="配置名称" align="center" prop="hvcName"/>
      <el-table-column label="Hive版本" align="center" prop="hvVersion"/>
      <el-table-column label="Hive元数据服务地址" align="center" prop="hvMetastoreUrl"/>
      <el-table-column label="hiveserver2连接地址" align="center" prop="hvServer2Url"/>
      <el-table-column label="hiveserver2连接用户名" align="center" prop="hvServer2Username"/>
      <el-table-column label="hiveserver2连接密码" align="center" prop="hvServer2Password"/>
      <el-table-column label="Hive元数据库-连接驱动" align="center" prop="hvMetadbDriver"/>
      <el-table-column label="Hive元数据库-连接地址" align="center" prop="hvMetadbUrl"/>
      <el-table-column label="Hive元数据库-连接用户名" align="center" prop="hvMetadbUsername"/>
      <el-table-column label="Hive元数据库-连接密码" align="center" prop="hvMetadbPassword"/>
      <el-table-column label="操作" align="center" fixed="right" width="110" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dmphive:config:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmphive:config:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Hive配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-tabs v-model="activeName" :tab-position="tabPosition">

          <el-tab-pane label="基本信息" name="hiveBase">
            <el-form-item label="配置名称" prop="hvcName">
              <el-input v-model="form.hvcName" placeholder="请输入配置名称"/>
            </el-form-item>
            <el-form-item label="Hive版本" prop="hvVersion">
              <el-input v-model="form.hvVersion" placeholder="请输入Hive版本"/>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="Hive元数据服务" name="metastore">
            <el-form-item label="Hive元数据服务地址" prop="hvMetastoreUrl">
              <el-input v-model="form.hvMetastoreUrl" placeholder="请输入Hive元数据服务地址"/>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="hiveserver2" name="hiveserver2">
            <el-form-item label="hiveserver2连接地址" prop="hvServer2Url">
              <el-input v-model="form.hvServer2Url" placeholder="请输入hiveserver2连接地址"/>
            </el-form-item>
            <el-form-item label="hiveserver2连接用户名" prop="hvServer2Username">
              <el-input v-model="form.hvServer2Username" placeholder="请输入hiveserver2连接用户名"/>
            </el-form-item>
            <el-form-item label="hiveserver2连接密码" prop="hvServer2Password">
              <el-input v-model="form.hvServer2Password" placeholder="请输入hiveserver2连接密码"/>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="Hive元数据库" name="metadb">
            <el-form-item label="Hive元数据库-连接驱动" prop="hvMetadbDriver">
              <el-input v-model="form.hvMetadbDriver" placeholder="请输入Hive元数据库-连接驱动"/>
            </el-form-item>
            <el-form-item label="Hive元数据库-连接地址" prop="hvMetadbUrl">
              <el-input v-model="form.hvMetadbUrl" placeholder="请输入Hive元数据库-连接地址"/>
            </el-form-item>
            <el-form-item label="Hive元数据库-连接用户名" prop="hvMetadbUsername">
              <el-input v-model="form.hvMetadbUsername" placeholder="请输入Hive元数据库-连接用户名"/>
            </el-form-item>
            <el-form-item label="Hive元数据库-连接密码" prop="hvMetadbPassword">
              <el-input v-model="form.hvMetadbPassword" placeholder="请输入Hive元数据库-连接密码" type="password" auto-complete="new-password"/>
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="isShowConfimBtn" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .el-form-item {
    margin-bottom: 16px; /*控制行高*/
  }

  .searchFormClass .el-input {
    width: 160px;
  }
</style>

<script>
  import {listConfig, getConfig, delConfig, addConfig, updateConfig} from "@/api/dmphive/config";

  export default {
    name: "Config",
    data() {
      return {
        // 是否显示确定/提交按钮,默认true显示
        isShowConfimBtn: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // Hive配置表格数据
        configList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          hvcName: null,
          hvVersion: null,
        },
        activeName: 'hiveBase',// tab默认激活的页签
        tabPosition: 'top',// tab方向
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          hvcName: [
            {required: true, message: "配置名称不能为空", trigger: "blur"}
          ],
          hvVersion: [
            {required: true, message: "Hive版本不能为空", trigger: "blur"}
          ],
          hvServer2Url: [
            {required: true, message: "hiveserver2连接地址不能为空", trigger: "blur"}
          ],
          hvServer2Username: [
            {required: true, message: "hiveserver2连接用户名不能为空", trigger: "blur"}
          ],
          hvMetadbDriver: [
            {required: true, message: "Hive元数据库-连接驱动不能为空", trigger: "blur"}
          ],
          hvMetadbUrl: [
            {required: true, message: "Hive元数据库-连接地址不能为空", trigger: "blur"}
          ],
          hvMetadbUsername: [
            {required: true, message: "Hive元数据库-连接用户名不能为空", trigger: "blur"}
          ],
          hvMetadbPassword: [
            {required: true, message: "Hive元数据库-连接密码不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建日期不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "更新时间不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询Hive配置列表 */
      getList() {
        this.loading = true;
        listConfig(this.queryParams).then(response => {
          this.configList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          hvcId: null,
          hvcName: null,
          hvVersion: null,
          hvMetastoreUrl: null,
          hvServer2Url: null,
          hvServer2Username: null,
          hvServer2Password: null,
          hvMetadbDriver: null,
          hvMetadbUrl: null,
          hvMetadbUsername: null,
          hvMetadbPassword: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.hvcId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加Hive配置";
        this.isShowConfimBtn = true;// 显示确定按钮
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const hvcId = row.hvcId || this.ids
        getConfig(hvcId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改Hive配置";
          this.isShowConfimBtn = true;// 显示确定按钮
        });
      },
      /** 查看操作 */
      showForm(row) {
        this.reset();
        const hvcId = row.hvcId || this.ids
        getConfig(hvcId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看Hive配置";
          this.isShowConfimBtn = false;// 隐藏确定按钮
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (!valid) {
            this.$modal.msgError("有内容未通过校验，请切换Tab查看!!");
          }else{
            if (this.form.hvcId != null) {
              updateConfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const hvcIds = row.hvcId || this.ids;
        this.$modal.confirm('是否确认删除Hive配置编号为"' + hvcIds + '"的数据项？').then(function () {
          return delConfig(hvcIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('dmphive/config/export', {
          ...this.queryParams
        }, `config_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
